package dao

import (
	"go-admin/global"
	"go-admin/internal/model"
	mr "go-admin/internal/model/api/model_request"
	_const "go-admin/internal/model/const"
	"go-admin/pkg/app"
	"time"
)

func (d *Dao) CreateJob(param *mr.CreateJobRequest) (string, error) {
	job := model.Job{
		CommonModel: model.CommonModel{
			Id:    global.GetId(),
			IsDel: _const.ISDEL,
		},
		IsBanned: _const.ISBANNED,
		JobName:  param.JobName,
		JobCode:  param.JobCode,
	}
	if param.Remark != "" {
		job.Remark = param.Remark
	}
	return job.Create(d.Engine)
}

// 查询总数
func (d *Dao) CountJob(param *mr.ListJobRequest) (int, error) {
	job := model.Job{
		IsBanned: param.IsBanned,
		JobName:  param.JobName,
	}
	return job.Count(d.Engine)
}

// 获取列表
func (d *Dao) GetJobList(param *mr.ListJobRequest, pageIndex, pageSize int) ([]*model.Job, error) {
	job := model.Job{
		IsBanned: param.IsBanned,
		JobName:  param.JobName,
	}
	pageOffset := app.GetPageOffset(pageIndex, pageSize)
	return job.List(d.Engine, pageOffset, pageSize)
}

func (d *Dao) UpdateJob(param *mr.UpdateJobRequest) error {
	job := model.Job{
		CommonModel: model.CommonModel{
			Id: param.Id,
		},
	}
	values := map[string]interface{}{
		"is_banned":   param.IsBanned,
		"modified_on": time.Now().Local().Unix(),
		"job_name":    param.JobName,
		"job_code":    param.JobCode,
	}
	if param.Remark != "" {
		values["remark"] = param.Remark
	}
	return job.Update(d.Engine, values)
}

func (d *Dao) DeleteJobS(ids []string) error {
	job := model.Job{}
	values := map[string]interface{}{
		"deleted_on": time.Now().Local().Unix(),
		"is_del":     _const.DEL,
	}
	return job.Deletes(d.Engine, ids, values)
}
func (d *Dao) ListAll() ([]*model.Job, error) {
	job := model.Job{}
	return job.ListAll(d.Engine)
}

func (d *Dao) GetJobByJobNameAndJobCode(param *mr.CreateJobRequest) (*model.Job, error) {
	job := model.Job{}
	return job.GetJobByJobNameAndJobCode(d.Engine, param.JobName, param.JobCode)
}

// 根据id获取详情
func (d *Dao) GetJobById(Id string) (*model.Job, error) {
	job := model.Job{
		CommonModel: model.CommonModel{Id: Id},
	}
	return job.GetJobById(d.Engine)
}
